package com.sk.sk_frame_cloud.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sk.sk_frame_cloud.entity.SysProFunInfo;
import com.sk.sk_frame_cloud.entity.SysProFunRelation;
import com.sk.sk_frame_cloud.mapper.SysProFunRelationMapper;
import com.sk.sk_frame_cloud.param.SysFunctionParam;
import com.sk.sk_frame_cloud.res.CommonResponse;
import com.sk.sk_frame_cloud.service.ISysProFunInfoService;
import com.sk.sk_frame_cloud.service.ISysProFunRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.sk_frame_cloud.service.ISysProManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 项目-功能绑定关系 服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2023-12-20
 */
@Service
public class SysProFunRelationServiceImpl extends ServiceImpl<SysProFunRelationMapper, SysProFunRelation> implements ISysProFunRelationService {

    @Resource
    private SysProFunRelationMapper  sysProFunRelationMapper;

    @Resource
    private ISysProFunInfoService iSysProFunInfoService;

    @Resource
    private FunctionsServiceImpl functionsService;

    @Resource
    private ISysProManageService iSysProManageService;

    @Override
    public void saveProFunRelation(String funSid, String proSid) {
        SysProFunRelation sysProFunRelation = new SysProFunRelation();

        sysProFunRelation.setSid(IdUtil.getSnowflakeNextIdStr());
        sysProFunRelation.setFunSid(funSid);
        sysProFunRelation.setProSid(proSid);
        sysProFunRelation.setCreateTime(LocalDateTime.now());

        this.save(sysProFunRelation);

    }

    @Override
    public CommonResponse getProBindingFuns(String proSid) {
        List<SysProFunInfo> funInfoList= sysProFunRelationMapper.getProBindingFuns(proSid);
        return CommonResponse.ok(funInfoList);
    }

    @Override
    public CommonResponse addBindingProFunction(SysFunctionParam functionParam) {

        String dbCode = iSysProManageService.getProCode(functionParam.getProSid());
        if (StringUtils.isBlank(dbCode)){
            return CommonResponse.error("proSid 不能为空");
        }

        //父项目中创建关联关系
        this.saveProFunRelation(functionParam.getFunSid(), functionParam.getProSid());
        //添加信息到子项目
        SysProFunInfo sysProFunInfo = iSysProFunInfoService.getById(functionParam.getFunSid());

        functionParam.setFunType(sysProFunInfo.getFunType());
        functionParam.setCode(sysProFunInfo.getFunCode());
        functionParam.setNickname(functionParam.getNickname());
        // TODO: 2023-12-20 操作子项目
        functionsService.operaFollowFunction(dbCode,functionParam);

        return CommonResponse.ok("绑定成功");
    }

    /**
     * 需要删除父项目的绑定关系，子项目的功能表，权限表
     * @param functionParam
     * @return
     */
    @Override
    public CommonResponse removeBindingProFunction(SysFunctionParam functionParam) {

        String dbCode = iSysProManageService.getProCode(functionParam.getProSid());
        if (StringUtils.isBlank(dbCode)){
            return CommonResponse.error("proSid 不能为空");
        }

        boolean remove = this.remove(new QueryWrapper<SysProFunRelation>().eq("pro_sid", functionParam.getProSid()).eq("fun_sid", functionParam.getFunSid()));

        //移除子项目中的信息
        functionsService.removeFollowFunctionAndPermission(dbCode,functionParam);

        return CommonResponse.ok("解绑成功");
    }
}
